package cn.com.egova.materialdesignexample.activity;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import cn.com.egova.materialdesignexample.R;

public class DrawerLayoutActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private LinearLayout llMain;
    private LinearLayout llDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.actiivty_drawer_layout);

        initToolbar();

        initView();
    }

    private void initToolbar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("DrawerLayout使用");
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setNavigationOnClickListener(new NavigationView.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    private void initView(){
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        llMain = (LinearLayout) findViewById(R.id.ll_main);
        llDrawer = (LinearLayout) findViewById(R.id.ll_drawer);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.drawer_layout_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case  R.id.action_toggle:
                if(drawerLayout.isDrawerOpen(llDrawer)){
                    drawerLayout.closeDrawer(Gravity.LEFT);  //指定是哪个drawerView,或者指定是哪个反向的drawerView都可以.
                }else{
                    drawerLayout.openDrawer(llDrawer);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
